<!--
 * @Description: 
 * @Version: V1.0.0
 * @Author: 周艳凯 484894717@qq.com
 * @Date: 2024-10-23 16:29:46
 * @LastEditors: 周艳凯 484894717@qq.com
 * @LastEditTime: 2024-10-23 18:13:37
 * @FilePath: slider-ticks.vue
 * Copyright 2024 Marvin, All Rights Reserved. 
 * 2024-10-23 16:29:46
-->
<template>
  <view :class="classNames">
    <view
      v-for="item in steps"
      :key="item"
      :style="getStyle(item.key)"
      :class="[
        'arco-slider-tick',
        { [`arco-slider-tick-active`]: item.isActive },
      ]"
    >
    </view>
  </view>
</template>

<script lang="ts">
import { computed, defineComponent } from "vue";
const SliderTicks = defineComponent({
  name: "SliderTicks",
  options: {
    // 虚拟化节点
    virtualHost: true,
  },
  props: {
    value: {
      type: Array,
      required: true,
    },
    step: {
      type: Number,
      required: true,
    },
    min: {
      type: Number,
      required: true,
    },
    max: {
      type: Number,
      required: true,
    },
    direction: {
      type: String,
      default: "horizontal",
    },
  },
  emits: ["moving", "movestart", "moveend"],
  setup(props) {
    const prefixCls = "arco-slider-ticks";
    const steps = computed(() => {
      const steps2 = [];
      const stepsLength = Math.floor((props.max - props.min) / props.step);
      for (let i = 0; i <= stepsLength; i++) {
        const stepVal = calculateStepVal(i, props.min, props.step);
        if (stepVal <= props.min || stepVal >= props.max) continue;
        steps2.push({
          key: stepVal,
          isActive: stepVal >= props.value[0] && stepVal <= props.value[1],
        });
      }
      return steps2;
    });

    const getStyle = (value) =>
      getPositionStyle(
        getOffsetPercent(value, [props.min, props.max]),
        props.direction,
      );

    const getOffsetPercent = (value, [min, max]) => {
      const percent = Math.max((value - min) / (max - min), 0);
      return `${Math.round(percent * 10000) / 100}%`;
    };

    const getPositionStyle = (offset, direction) => {
      return direction === "vertical" ? { bottom: offset } : { left: offset };
    };

    const classNames = computed(() => {
      return [prefixCls];
    });

    function calculateStepVal(i, min, step) {
      const product = i * step;
      const result = min + product;
      return result;
    }

    return {
      prefixCls,
      classNames,
      steps,
      getStyle,
    };
  },
});
export default SliderTicks;
</script>
